package com.kzf.controller;

import com.kzf.anno.Log;
import com.kzf.pojo.*;
import com.kzf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getStudent(StudentQueryParam studentQueryParam) {
        StudentResult<Student> studentResult = studentService.getStudent(studentQueryParam);
        return Result.success(studentResult);
    }

    //此处请求参数的格式样例为/students/1,2,3 而不是/students?ids=1,2,3
    //注意区分，因此这里不能使用@RequestParam 集合接收
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable String ids){
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                        .collect(Collectors.toList());
        studentService.deleteByIds(idList);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getStudentById(@PathVariable Integer id) {
        StudentDTO studentDTO = studentService.getStudentById(id);
        return Result.success(studentDTO);
    }
    @Log
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return Result.success();
    }

    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.updateScore(id,score);
        return Result.success();
    }

}
